package zero.tech.games.trade.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import zero.tech.core.data.Resp;
import zero.tech.core.util.DatetimeUtil;
import zero.tech.games.common.ClubMemberUtil;
import zero.tech.games.common.ClubWater;
import zero.tech.games.common.Code;
import zero.tech.games.common.GoldChangeType;
import zero.tech.games.common.QueueConsts;
import zero.tech.games.entity.games.ClubMember;
import zero.tech.games.entity.games.Translog;
import zero.tech.games.entity.log.LogGoldChange;
import zero.tech.games.trade.data.UpdateVo;
import zero.tech.games.trade.mapper.ClubMemberMapper;
import zero.tech.games.trade.mapper.TranslogMapper;
import zero.tech.games.trade.service.api.ITradeService;
import zero.tech.redis.lock.RedisLockUtil;
import zero.tech.redis.lock.RedisLockUtil.LockMethod;
import zero.tech.redis.queue.QueueUtil;

@Service
public class TradeServiceImpl implements ITradeService{
	@Autowired
	private RedisLockUtil lockUtil;
	@Autowired
	private TranslogMapper translogMapper;
	@Autowired
	private ClubMemberMapper clubMemberMapper;
	@Autowired
	private ClubMemberUtil clubMemberUtil;
	@Autowired
	private QueueUtil queueUtil;
	@Override
	@Transactional
	public Resp gameUpdateBatch(String order_id, int club_id, int game_id, String room_id, int rule_id,int room_times, String room_name,int total_times,
			String joiners, String joiners_ai, Map<Integer, Long> gold_change_map, Map<Integer, Long> water_map) {
		if(gold_change_map.isEmpty()) {
			return Resp.build(Code.OK, "不用处理");
		}
		return lockUtil.lockMethod(new LockMethod<Resp>() {
			@Override
			public Resp lockRun() {
				Date now=new Date();
				String day_str=DatetimeUtil.parseDateToString("yyyy-MM-dd", now);
				Translog translog=translogMapper.getTranslog(order_id);
				if(translog==null) {
					translog=new Translog(order_id, 0);
					translogMapper.insert(translog);
				}else {
					if(translog.getStatus()==1) {
						//已处理过
						return Resp.build(Code.OK, "已处理过");
					}
				}
				List<UpdateVo> list = new ArrayList<UpdateVo>();
				String sql = "";
				for (Entry<Integer, Long> entry : gold_change_map.entrySet()) {
					Integer uid = entry.getKey();
					Long change = entry.getValue();
					UpdateVo updateVo = new UpdateVo(club_id,uid, change);
					list.add(updateVo);
					sql += " select * from club_member where club_id="+club_id+" and uid=" + uid + " union all";
				}
				clubMemberMapper.updateTeaBatch(list);
				if(!StringUtils.isBlank(sql)) {
					sql=sql.substring(0, sql.length()-"union all".length());
				}
				List<ClubMember> clubMembers = clubMemberMapper.execute(sql);
				//开始记录日志
				List<LogGoldChange> logGoldChanges = new ArrayList<>();
				List<ClubWater> clubWaters=new ArrayList<>();
				for(ClubMember clubMember:clubMembers) {
					int club_id=clubMember.getClub_id();
					int uid=clubMember.getUid();
					LogGoldChange logGoldChange = new LogGoldChange();
					logGoldChange.setClub_id(club_id);
					logGoldChange.setUid(uid);
					logGoldChange.setGame_id(game_id);
					logGoldChange.setRoom_id(room_id);
					logGoldChange.setRoom_times(room_times);
					logGoldChange.setRoom_name(room_name);
					logGoldChange.setTotal_times(total_times);
					logGoldChange.setJoiners(joiners_ai);
					logGoldChange.setJoiners_ai(joiners_ai);
					long change_num=gold_change_map.get(uid);
					logGoldChange.setChange_num(change_num);
					logGoldChange.setChange_type(GoldChangeType.GAME_CHANGE);
					logGoldChange.setChange_desc("游戏变化");
					long water=water_map.get(uid)==null?0:water_map.get(uid);
					logGoldChange.setWater(water);
					logGoldChange.setCur_tea(clubMember.getTea());
					logGoldChange.setCur_safety(clubMember.getSafety());
					logGoldChange.setCur_frozen(clubMember.getFrozen());
					logGoldChange.setDay_str(day_str);
					logGoldChange.setCreate_stamp(now);
					logGoldChanges.add(logGoldChange);
					clubWaters.add(new ClubWater(club_id, game_id,room_id,rule_id,uid, water));
				}
				translog.setStatus(1);
				translogMapper.update(translog);
				clubMemberUtil.setClubMemberBatch(club_id,clubMembers);
				queueUtil.putQueueBatch(QueueConsts.QUEUE_LOG_GOLD_CHANGE, logGoldChanges);
				queueUtil.putQueueBatch(QueueConsts.QUEUE_CLUB_WATER, clubWaters);
				//更新缓存
				return Resp.build(Code.OK, "处理成功");
			}
		}, order_id, 30);
	}

}
